<?php

/*
 *	FalconKMS Front End Functions File
 *	Version: 1.1
 *	Author: Geoff Wilson (contact@gawilson.net)
 *
 *	Provides functions to be shared with other code, these functions are used to display data
 *	on the client facing part of the website
*/

include_once "killclass.php";

function drawAllianceLosses($allianceID, $numberToDraw, $killIndex)
{
	include_once "config.php";

	echo ("<tr><td>");

	// Get the kills from the database
	$sql = "SELECT `killID` FROM `kb_kill` WHERE `allianceID` = '" . $allianceID . "' ORDER BY `killTime` DESC LIMIT " . $killIndex . "," . $numberToDraw;
	$queryResult = mysql_query($sql);

	if (mysql_num_rows($queryResult) > 0)
	{
		drawDetails($queryResult);

		$sql = "SELECT COUNT(`killID`) FROM `kb_kill` WHERE `allianceID` = '" . $allianceID . "'";

		$killResource = mysql_query($sql);
		$totalKills = mysql_result($killResource, 0);
		$newKillIndex = $totalKills  - $killIndex;

		$moreToShow = FALSE;

		if ($newKillIndex > $numberToDraw) $moreToShow = TRUE;

		echo('<table style="width:100%; table-layout:fixed;"><tr><td style="width:50%;">');
		if ($killIndex > 0) printf("<a href=\"?id=%s&kn=%s\"><img border=\"0\"  src=\"images/previousKills.png\" /></a>", $allianceID, ($killIndex - $numberToDraw));
		echo('</td><td style="text-align:right; width:50%;">');
		if ($moreToShow == TRUE) printf("<a href=\"?id=%s&kn=%s\"><img border=\"0\"  src=\"images/nextKills.png\" /></a>", $allianceID, ($killIndex + $numberToDraw));
		echo('</td></tr></table>');

	}
	else
	{
		echo ('<p>No Losses</p>');
	}

	echo ("</td></tr>");

}

function drawAllianceKills($allianceID, $numberToDraw, $killIndex)
{
	include_once "config.php";

	echo ("<tr><td>");

	// Get the kills from the database
	$sql = "SELECT `killID` FROM `kb_involved` WHERE `allianceID` = '" . $allianceID . "' GROUP BY `killID` ORDER BY `involvedTime`  DESC LIMIT " . $killIndex . "," . $numberToDraw;
	$queryResult = mysql_query($sql);

	if (mysql_num_rows($queryResult) > 0)
	{
		drawDetails($queryResult);

		$sql = "SELECT COUNT(`killID`) FROM `kb_involved` WHERE `allianceID` = '" . $allianceID . "'";

		$killResource = mysql_query($sql);
		$totalKills = mysql_result($killResource, 0);
		$newKillIndex = $totalKills  - $killIndex;

		$moreToShow = FALSE;

		if ($newKillIndex > $numberToDraw) $moreToShow = TRUE;

		echo('<table style="width:100%; table-layout:fixed;"><tr><td style="width:50%;">');
		if ($killIndex > 0) printf("<a href=\"?id=%s&kn=%s\"><img border=\"0\"  src=\"images/previousKills.png\" /></a>", $allianceID, ($killIndex - $numberToDraw));
		echo('</td><td style="text-align:right; width:50%;">');
		if ($moreToShow == TRUE) printf("<a href=\"?id=%s&kn=%s\"><img border=\"0\"  src=\"images/nextKills.png\" /></a>", $allianceID, ($killIndex + $numberToDraw));
		echo('</td></tr></table>');

	}
	else
	{
		echo ('<p>No Kills</p>');
	}
}

function drawCorpLosses($corpID, $numberToDraw, $killIndex)
{
	include_once "config.php";

	echo ("<tr><td>");

	// Get the kills from the database
	$sql = "SELECT `killID` FROM `kb_kill` WHERE `corpID` = '" . $corpID . "' ORDER BY `killTime` DESC LIMIT " . $killIndex . "," . $numberToDraw;
	$queryResult = mysql_query($sql);

	if (mysql_num_rows($queryResult) > 0)
	{
		drawDetails($queryResult);

		$sql = "SELECT COUNT(`killID`) FROM `kb_kill` WHERE `corpID` = '" . $corpID . "'";

		$killResource = mysql_query($sql);
		$totalKills = mysql_result($killResource, 0);
		$newKillIndex = $totalKills  - $killIndex;

		$moreToShow = FALSE;

		if ($newKillIndex > $numberToDraw) $moreToShow = TRUE;

		echo('<table style="width:100%; table-layout:fixed;"><tr><td style="width:50%;">');
		if ($killIndex > 0) printf("<a href=\"?id=%s&kn=%s\"><img border=\"0\"  src=\"images/previousKills.png\" /></a>", $corpID, ($killIndex - $numberToDraw));
		echo('</td><td style="text-align:right; width:50%;">');
		if ($moreToShow == TRUE) printf("<a href=\"?id=%s&kn=%s\"><img border=\"0\"  src=\"images/nextKills.png\" /></a>", $corpID, ($killIndex + $numberToDraw));
		echo('</td></tr></table>');

	}
	else
	{
		echo ('<p>No Losses</p>');
	}

	echo ("</td></tr>");

}

function drawCorpKills($corpID, $numberToDraw, $killIndex)
{
	include_once "config.php";

	echo ("<tr><td>");

	// Get the kills from the database
	$sql = "SELECT `killID` FROM `kb_involved` WHERE `corpID` = '" . $corpID . "' GROUP BY `killID` ORDER BY `involvedTime`  DESC LIMIT " . $killIndex . "," . $numberToDraw;
	$queryResult = mysql_query($sql);

	if (mysql_num_rows($queryResult) > 0)
	{
		drawDetails($queryResult);

		$sql = "SELECT COUNT(`killID`) FROM `kb_involved` WHERE `corpID` = '" . $corpID . "'";

		$killResource = mysql_query($sql);
		$totalKills = mysql_result($killResource, 0);
		$newKillIndex = $totalKills  - $killIndex;

		$moreToShow = FALSE;

		if ($newKillIndex > $numberToDraw) $moreToShow = TRUE;

		echo('<table style="width:100%; table-layout:fixed;"><tr><td style="width:50%;">');
		if ($killIndex > 0) printf("<a href=\"?id=%s&kn=%s\"><img border=\"0\"  src=\"images/previousKills.png\" /></a>", $corpID, ($killIndex - $numberToDraw));
		echo('</td><td style="text-align:right; width:50%;">');
		if ($moreToShow == TRUE) printf("<a href=\"?id=%s&kn=%s\"><img border=\"0\"  src=\"images/nextKills.png\" /></a>", $corpID, ($killIndex + $numberToDraw));
		echo('</td></tr></table>');

	}
	else
	{
		echo ('<p>No Kills</p>');
	}

	echo ("</td></tr>");

}

function drawPlayerLosses($playerID, $numberToDraw, $killIndex)
{
	include_once "config.php";

	echo ("<tr><td>");

	// Get the kills from the database
	$sql = "SELECT `killID` FROM `kb_kill` WHERE `playerID` = '" . $playerID . "' ORDER BY `killTime` DESC LIMIT " . $killIndex . "," . $numberToDraw;
	$queryResult = mysql_query($sql);

	if (mysql_num_rows($queryResult) > 0)
	{
		drawDetails($queryResult);

		$sql = "SELECT COUNT(`killID`) FROM `kb_kill` WHERE `playerID` = '" . $playerID . "'";

		$killResource = mysql_query($sql);
		$totalKills = mysql_result($killResource, 0);
		$newKillIndex = $totalKills  - $killIndex;

		$moreToShow = FALSE;

		if ($newKillIndex > $numberToDraw) $moreToShow = TRUE;

		echo('<table style="width:100%; table-layout:fixed;"><tr><td style="width:50%;">');
		if ($killIndex > 0) printf("<a href=\"?id=%s&kn=%s\"><img border=\"0\"  src=\"images/previousKills.png\" /></a>", $playerID, ($killIndex - $numberToDraw));
		echo('</td><td style="text-align:right; width:50%;">');
		if ($moreToShow == TRUE) printf("<a href=\"?id=%s&kn=%s\"><img border=\"0\"  src=\"images/nextKills.png\" /></a>", $playerID, ($killIndex + $numberToDraw));
		echo('</td></tr></table>');

	}
	else
	{
		echo ('<p>No Losses</p>');
	}

	echo ("</td></tr>");

}

function drawPlayerKills($playerID, $numberToDraw, $killIndex)
{
	include_once "config.php";

	echo ("<tr><td>");

	// Get the kills from the database
	$sql = "SELECT `killID` FROM `kb_involved` WHERE `playerID` = '" . $playerID . "' GROUP BY `killID` ORDER BY `involvedTime`  DESC LIMIT " . $killIndex . "," . $numberToDraw;
	$queryResult = mysql_query($sql);

	if (mysql_num_rows($queryResult) > 0)
	{
		drawDetails($queryResult);

		$sql = "SELECT COUNT(`killID`) FROM `kb_involved` WHERE `playerID` = '" . $playerID . "'";

		$killResource = mysql_query($sql);
		$totalKills = mysql_result($killResource, 0);
		$newKillIndex = $totalKills  - $killIndex;

		$moreToShow = FALSE;

		if ($newKillIndex > $numberToDraw) $moreToShow = TRUE;

		echo('<table style="width:100%; table-layout:fixed;"><tr><td style="width:50%;">');
		if ($killIndex > 0) printf("<a href=\"?id=%s&kn=%s\"><img border=\"0\"  src=\"images/previousKills.png\" /></a>", $playerID, ($killIndex - $numberToDraw));
		echo('</td><td style="text-align:right; width:50%;">');
		if ($moreToShow == TRUE) printf("<a href=\"?id=%s&kn=%s\"><img border=\"0\"  src=\"images/nextKills.png\" /></a>", $playerID, ($killIndex + $numberToDraw));
		echo('</td></tr></table>');

	}
	else
	{
		echo ('<p>No Kills</p>');
	}

	echo ("</td></tr>");

}

function drawAllKills($numberToDraw, $killIndex) // gets all kills in order of kill date
{
	include_once "config.php";

//	echo ("<tr><td>");

	// Get the kills from the database
	$sql = "SELECT `killID` FROM `kb_kill` WHERE (`shipID` < 127) OR (`shipID` > 131) ORDER BY `killTime` DESC LIMIT " . $killIndex . "," . $numberToDraw;
	$queryResult = mysql_query($sql);

	if (mysql_num_rows($queryResult) > 0)
	{
		drawDetails($queryResult);

		$sql = "SELECT COUNT(`killID`) FROM `kb_kill`";

		$killResource = mysql_query($sql);
		$totalKills = mysql_result($killResource, 0);
		$newKillIndex = $totalKills  - $killIndex;

		$moreToShow = FALSE;

		if ($newKillIndex > $numberToDraw) $moreToShow = TRUE;

		echo('<table style="width:100%; table-layout:fixed;"><tr><td style="width:50%;">');
		if ($killIndex > 0) printf("<a href=\"?kn=%s\"><img border=\"0\"  src=\"images/previousKills.png\" /></a>", ($killIndex - $numberToDraw));
		echo('</td><td style="text-align:right; width:50%;">');
		if ($moreToShow == TRUE) printf("<a href=\"?kn=%s\"><img border=\"0\"  src=\"images/nextKills.png\" /></a>", ($killIndex + $numberToDraw));
		echo('</td></tr></table>');

	}
	else
	{
		echo ('<p>No Kills</p>');
	}

//	echo ("</td></tr>");

}

// Pass a reference, reduce memory consumption with copying a huge amount of data
function drawDetails(&$queryResult)
{
	$lastDate = NULL;
       
	while ($killDetails = mysql_fetch_array($queryResult))
	{
		$killMail = new Kill($killDetails['killID']);
		if ($lastDate != date("d-m-Y", $killMail->timestamp))
		{
			if ($lastDate != NULL) echo ('</table><table id="killTable" style="width:100%;">');
                        else echo ('<table id="killTable" style="width:100%;">');

			// Need to write out new date line
			printf ('<tr style="height:20px;"><td id="dateHeader" colspan="5"><b>%s</b></td></tr>', date("l, F jS Y", $killMail->timestamp));
			//echo ('<table id="killTable" style="width:100%;">');
			echo ('<tr style="height:20px;">
					<th></th>
					<th style="width:20%;">Ship Lost</th>
					<th style="width:30%;">Victim</th>
					<th style="width:30%;">Killed By</th>
					<th style="width:20%;">System</th>
			</tr>');

			// Set Last Date this date
			$lastDate = date("d-m-Y", $killMail->timestamp);
		}

		if (floatval($killMail->solarSystemSecurity) < 0)
		{
			$killMail->solarSystemSecurity = "0.0";
		}
		else
		{
			$killMail->solarSystemSecurity = substr($killMail->solarSystemSecurity,0,3);
		}

		$filename = "ships/" . $killMail->shipGraphicID . ".png";

		if (!file_exists($filename))
		{
			$filename = "ships/noImage.png";
		}

		if (($killMail->shipClassID == 1) || ($killMail->shipClassID == 20))
		{
			$imageBorder = "goldShipImage";
		}
		else
		{
			$imageBorder = "shipImage";
		}

		printf ('<tr style="background-color:#ABBEBE;" onmouseover="this.style.backgroundColor=\'#FFCC00\';" onmouseout="this.style.backgroundColor=\'#ABBEBE\';" onclick="location.href=\'showkill.php?killid=%s\';">
					<td style="text-align:center;"><img src="%s" class="%s"/></td>
					<td>%s<br/><em>%s</em></td>
					<td><a href="plosses.php?id=%s"><b>%s</b></a><br/>
						<a href="closses.php?id=%s"><em>%s</em></a><br/>
						<a href="alosses.php?id=%s"><em>%s</em></a></td>
					<td><a href="pkills.php?id=%s"><b>%s</b></a><br/>
						<a href="ckills.php?id=%s"><em>%s</em></a><br/>
						<a href="akills.php?id=%s"><em>%s</em></a></td>
					<td>%s<br/>%s</td>
		</tr>',
		$killMail->killID,
		$filename, $imageBorder,
		$killMail->ship, $killMail->shipClass,
		$killMail->victimID, $killMail->victim,
		$killMail->corpID, substr($killMail->corp,0,25),
		$killMail->allianceID, substr($killMail->alliance,0,25),
		$killMail->involvedParties[$killMail->finalBlowIndex]->playerID,
		$killMail->involvedParties[$killMail->finalBlowIndex]->player,
		$killMail->involvedParties[$killMail->finalBlowIndex]->corpID,
		substr($killMail->involvedParties[$killMail->finalBlowIndex]->corp,0,25),
		$killMail->involvedParties[$killMail->finalBlowIndex]->allianceID,
		substr($killMail->involvedParties[$killMail->finalBlowIndex]->alliance,0,25),
		$killMail->solarSystem, $killMail->solarSystemSecurity);

	}
	echo ("</table>");
}